package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.Salary;
import com.marktotop.mapper.SalaryMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.ISalaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javafx.scene.chart.XYChart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author marktotop
 * @since 2021-05-23
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements ISalaryService {
   @Autowired
   private SalaryMapper salaryMapper;
    /**
     * 分页显示所有工资账套信息
     * @return
     */

    @Override
    public ResultBean findAllByPage(Integer currentPage, Integer size, Salary salary) {
      //开启分页
        Page<Salary> page = new Page<>(currentPage, size);
        IPage<Salary> salaryByPage = salaryMapper.findAllByPage(page, salary);
        if (null==salaryByPage){
            return ResultBean.result(ResultCode.NON_RESULT);
        }else{
            return ResultBean.ok().message("获取工资套账信息成功！")
                    .data("total",salaryByPage.getTotal())
                    .data("salarys",salaryByPage.getRecords());
        }
    }
    /**
     * 添加账套信息
     * @param salary
     */
    @Override
    public ResultBean addSalary(Salary salary) {
        // 添加
       salary.setCreateDate(LocalDateTime.now());
       int flag= salaryMapper.addSalary(salary);
       if (flag==1){
           return ResultBean.ok().message("添加套账成功！");
       }
       return ResultBean.error().message("添加套账失败！");
    }
}
